<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增用户</title>
</head>
<script src="webjars/jquery/3.2.0/jquery.js"></script>
<body>
<!--新增用户-->
<div>
    <p id="userForm">

    <p style="width: 300px;margin: 0 auto;color: red">
        &nbsp; &nbsp;&nbsp; &nbsp;用户： <input type="text" name="uName" placeholder="请输入用户名" th:value="${user.uName}"><br><br>
        &nbsp; &nbsp;&nbsp; &nbsp;密码：<input type="password" name="uPwd" th:value="${user.uPwd}" placeholder="请输入密码"><br><br>
        确认密码：<input type="password" name="confirmPwd" id="confirmPwd" th:value="${user.uPwd}"
                    placeholder="请确认密码"><br><br>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="checkbox" name="uType" id="uType" th:value="${user.uType}"
                                                 placeholder="">
        管理员 <br><br>
        &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black">备注：</span><input type="text" th:value="${user.uMemo}"
                                                                                   name="uMemo"
                                                                                   placeholder=""><br><br>
        <!--  下划线-->
    <hr>
    <p style="text-align: right">
        <input type="button" value="返回列表" onclick="redirectToPage()">
        <input type="submit" onclick="insert()"
               value="修改"><br>
    </p>

    </p>
    </p>
</div>
</body>
<script th:inline="javascript">

    var carTypes = [[${user.uType}]];

    var uCode = [[${user.uCode}]];

    var uName = [[${user.uName}]];

    var uMemos = [[${user.uMemo}]]; 

    // 页面跳转函数
    function redirectToPage() {
        window.location.href = "http://localhost:8080/selectAll";
    };

    $(function () {
        // 复选框
        if (carTypes == 2) {
            $("input[name=uType]").attr("checked", true);
        }
    })

    function insert() {
        // 校验
        var uName = $("input[name=uName]").val();
        var uPwd = $("input[name=uPwd]").val();
        if (uName == "" || uPwd == "") {
            alert("用户名或密码不能为空");
            return false;
        }
        var uType = "";
        // 登录用户权限 type == 2 管理员
        var type = sessionStorage.getItem("uType");
        if (type == 2) {
            if ($('#uType').prop('checked')) {
                // 复选框被选中
                uType = 2;
            } else {
                // 复选框未被选中
                uType = 1;
            }
        } else {
            uType = 1;
        }
        // 选中下拉框按钮才有赋值为2

        // 提交事件
        // 验证密码
        var pwd = $("input[name=uPwd]").val();
        var confirmPwd = $("input[name=confirmPwd]").val();
        if (pwd != confirmPwd) {
            alert("两次密码不一致");
            return false;
        }
        var item = {
            uName: $("input[name=uName]").val(),
            uPwd: $("input[name=uPwd]").val(),
            uType: uType,
            uMemo: $("input[name=uMemo]").val()
        }
        if (uCode == sessionStorage.getItem("uId") && type == 2) {
            item.uName = uName;
            item.uType = carTypes;

        }
        $.post(
            "http://localhost:8080/update",
            {
                uCode: uCode,
                uName: item.uName,
                uPwd: item.uPwd,
                uType: item.uType,
                uMemo: item.uMemo
            },
            function (data) {
                alert("保存成功");
                if (data == "1") {
                    window.location.href = "http://localhost:8080/selectAll";
                }
            }
        )
    }

</script>
</html>